package com.sanpu.kxe.business.member.presenter;

import android.app.Activity;
import android.text.TextUtils;

import com.sanpu.kxe.business.main.bean.UserCenterOutput;
import com.sanpu.kxe.business.main.provider.IMainProvider;
import com.sanpu.kxe.business.member.bean.MemberProductRateOutput;
import com.sanpu.kxe.business.member.contract.MemberProductRateContract;
import com.sanpu.kxe.business.member.model.MemberModel;
import com.sanpu.kxe.common.cache.CacheManage;
import com.sanpu.kxe.common.cache.CacheModel;
import com.sanpu.kxe.common.interfaces.CommonConfig;
import com.sanpu.kxe.common.interfaces.DialogViewListener;
import com.sanpu.kxe.utils.DialogUtil;
import com.sanpu.kxe.utils.PublicProviderUtil;

import java.util.List;

import rx.Observer;
import rx.Subscription;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Description：会员产品费率
 * Created by 薛勇军 on 2019/4/15.
 */

public class MemberProductRatePresenter implements MemberProductRateContract.Presenter {

    private MemberProductRateContract.View mView;

    private CompositeSubscription mSubscription;

    public MemberProductRatePresenter(MemberProductRateContract.View mView) {
        this.mView = mView;
        mSubscription = new CompositeSubscription();
    }

    @Override
    public void productRateList() {
        mView.showLoadingDialog(true);
        Subscription subscription = MemberModel.getInstance().productRateList()
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<List<MemberProductRateOutput>>() {
                    @Override
                    public void onCompleted() {
                        mView.dismissLoadingDialog();
                    }

                    @Override
                    public void onError(Throwable e) {
                        mView.dismissLoadingDialog();
                        mView.showErrorText(e);
                        mView.showDataView(null);
                    }

                    @Override
                    public void onNext(List<MemberProductRateOutput> memberProductRateOutputs) {
                        mView.showDataView(memberProductRateOutputs);
                    }
                });
        mSubscription.add(subscription);
    }

    @Override
    public boolean isAuthenPass() {
        final UserCenterOutput centerOutput = CacheManage.getInstance().getCache(CacheModel.USER_CENTER_KEY);
        if (centerOutput != null) {
            if (!TextUtils.equals(CommonConfig.AUTHEN_PASS, centerOutput.getChkSts())) {
                String btnText = "", contentText = "";
                switch (centerOutput.getChkSts()) {
                    case CommonConfig.AUTHEN_NOTSUB:
                        btnText = "去认证";
                        contentText = "升级会员前请先完成认证";
                        break;
                    case CommonConfig.AUTHEN_AUDIT:
                        btnText = "确定";
                        contentText = "您的实名认证正在审核中，请稍后";
                        break;
                    case CommonConfig.AUTHEN_NOPASS:
                        btnText = "重新认证";
                        contentText = "您的实名认证审核失败，请重新认证";
                        break;
                    case CommonConfig.AUTHEN_NOCOMPLETION:
                        btnText = "去补全";
                        contentText = "需要补全资料才能升级会员";
                        break;
                }
                DialogUtil.getInstance().dialogNoTitletTwoBtnHaveCallBack(mView.getContext(), contentText, "取消", btnText,
                        new DialogViewListener() {
                            @Override
                            public void btnRightConfirm() {
                                if (!TextUtils.equals(CommonConfig.AUTHEN_AUDIT, centerOutput.getChkSts())) {
                                    openCertificationActivity();
                                } else {
                                    ((Activity) mView.getContext()).finish();
                                }
                            }

                            @Override
                            public void btnLeftCancel() {
                                ((Activity) mView.getContext()).finish();
                            }
                        });
                return false;
            }
            return true;
        }
        return false;
    }

    /**
     * 去认证
     */
    private void openCertificationActivity() {
        IMainProvider mainProvider = PublicProviderUtil.getProvider(IMainProvider.class);
        if (mainProvider != null) {
            mainProvider.openCertificationActivity(mView.getContext(), true);
        }
    }

    @Override
    public void onDestroyPresenter() {
        if (mSubscription != null && !mSubscription.isUnsubscribed()) {
            mSubscription.unsubscribe();
            mSubscription = null;
        }
        if (mView != null) {
            mView = null;
        }
    }
}
